using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Marlimer.Common;
using Marlimer.Common.EntidadesGrilla; 

namespace Marlimer.Data.Comercial
{
    public class com_PrecioCanal
    {

        public DataTable consultaHistorialPrecioCanal(int ccod_canal, int ccod_condvta, int ccod_mone, int ccod_prod)
        {
            SqlCommand sqlcmd = new SqlCommand();
            SqlDataAdapter da = new SqlDataAdapter();
            DataSet ds = new DataSet();

            sqlcmd.CommandText = "spsConsultaHistorialPrecioCanal";
            sqlcmd.CommandType = CommandType.StoredProcedure;
            sqlcmd.Connection = Session.getConexion();

            sqlcmd.Parameters.Add("@ccod_canal", SqlDbType.Int);
            sqlcmd.Parameters.Add("@ccod_condvta", SqlDbType.Int);
            sqlcmd.Parameters.Add("@ccod_mone", SqlDbType.Int);
            sqlcmd.Parameters.Add("@ccod_prod", SqlDbType.Int);

            sqlcmd.Parameters["@ccod_canal"].Value = ccod_canal;
            sqlcmd.Parameters["@ccod_condvta"].Value = ccod_condvta;
            sqlcmd.Parameters["@ccod_mone"].Value = ccod_mone;
            sqlcmd.Parameters["@ccod_prod"].Value = ccod_prod;

            da.SelectCommand = sqlcmd;
            da.Fill(ds);

            return ds.Tables[0];
        }

        public DataTable consultaPrecioCanal(int ccod_canal, int ccod_condvta, int ccod_mone, int ccod_grpvta)
        {
            SqlCommand sqlcmd = new SqlCommand();
            SqlDataAdapter da = new SqlDataAdapter();
            DataSet ds = new DataSet();

            sqlcmd.CommandText = "spsMostrarPreciosCanal";
            sqlcmd.CommandType = CommandType.StoredProcedure;
            sqlcmd.Connection = Session.getConexion();

            sqlcmd.Parameters.Add("@ccod_canal", SqlDbType.Int);
            sqlcmd.Parameters.Add("@ccod_condvta", SqlDbType.Int);
            sqlcmd.Parameters.Add("@ccod_mone", SqlDbType.Int);
            sqlcmd.Parameters.Add("@ccod_grpvta", SqlDbType.Int);

            sqlcmd.Parameters["@ccod_canal"].Value = ccod_canal;
            sqlcmd.Parameters["@ccod_condvta"].Value = ccod_condvta;
            sqlcmd.Parameters["@ccod_mone"].Value = ccod_mone;
            sqlcmd.Parameters["@ccod_grpvta"].Value = ccod_grpvta;

            da.SelectCommand = sqlcmd;
            da.Fill(ds);

            return ds.Tables[0];
        }

        public resulMsgError guardarPrecioCanal(List<InfoPrecioCanalSave> precs)
        {
            resulMsgError resul = new resulMsgError(false, "");
            SqlCommand sqlcmd = new SqlCommand(); 
            SqlTransaction transac = Session.getConexion().BeginTransaction("saveprec");
  
            try
            {
                sqlcmd.CommandText = "spiaPrecioCanal";
                sqlcmd.CommandType = CommandType.StoredProcedure;
                sqlcmd.Connection = Session.getConexion();
                sqlcmd.Transaction = transac;

                sqlcmd.Parameters.Add("@ccod_itm", SqlDbType.Int);
                sqlcmd.Parameters.Add("@ccod_prod", SqlDbType.Int);
                sqlcmd.Parameters.Add("@ccod_condvta", SqlDbType.Int);
                sqlcmd.Parameters.Add("@ccod_canal", SqlDbType.Int);
                sqlcmd.Parameters.Add("@ccod_mone", SqlDbType.Int);
                sqlcmd.Parameters.Add("@npre_vta", SqlDbType.Decimal);
                sqlcmd.Parameters.Add("@npre_vtaliq", SqlDbType.Decimal);
                sqlcmd.Parameters.Add("@npre_vtamin", SqlDbType.Decimal);
                sqlcmd.Parameters.Add("@cstatus", SqlDbType.NChar,1);
                sqlcmd.Parameters.Add("@cpc_crea", SqlDbType.NVarChar, 50);
                sqlcmd.Parameters.Add("@cuser_crea", SqlDbType.Int);

                //establecer los valores fijos
                sqlcmd.Parameters["@cpc_crea"].Value = Session.getComputerName();
                sqlcmd.Parameters["@cuser_crea"].Value = Session.getInfousuario().ccod_user;

                foreach (InfoPrecioCanalSave pre in precs)
                {
                    sqlcmd.Parameters["@ccod_itm"].Value = pre.ccod_itm;
                    sqlcmd.Parameters["@ccod_prod"].Value = pre.ccod_prod;
                    sqlcmd.Parameters["@ccod_condvta"].Value = pre.ccod_condvta;
                    sqlcmd.Parameters["@ccod_canal"].Value = pre.ccod_canal;
                    sqlcmd.Parameters["@ccod_mone"].Value = pre.ccod_mone;
                    sqlcmd.Parameters["@npre_vta"].Value = pre.npre_vta;
                    sqlcmd.Parameters["@npre_vtaliq"].Value = pre.npre_vtaliq;
                    sqlcmd.Parameters["@npre_vtamin"].Value = pre.npre_vtamin;
                    sqlcmd.Parameters["@cstatus"].Value = pre.cstatus;

                    sqlcmd.ExecuteNonQuery(); 
                }
                transac.Commit();
                resul.message = "Se guardaron todos los precios satisfactoriamente";
                resul.resul = true;
            }
            catch (Exception ex)
            {
                transac.Rollback();
                resul.message = ex.Message; 
            }
            return resul;
        }
    }
}
